package edu.unice.simul.vue.initialisation;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import edu.unice.simul.controleur.initialisation.InitialisationControleur;
import edu.unice.simul.listeners.PieceListener;
import edu.unice.simul.util.Dim;
import edu.unice.simul.vue.initialisation.modes_dessin.DrawToolBar;

/**
 * Fenêtre d'initialisation (déssin) de la simulation
 * 
 * @author Romain Ciaccafava
 * 
 */
public class InitialisationFrame extends JFrame {
	private static final long serialVersionUID = 5380961716683027103L;
	private DrawPanel piecePanel;
	private DrawToolBar modeChooser;
	private InitialisationControleur controleur;

	public InitialisationFrame(InitialisationControleur controleur, Dim dim) {
		this.controleur = controleur;
		this.piecePanel = new DrawPanel(controleur, dim);
		this.modeChooser = new DrawToolBar(piecePanel, controleur);
		buildUI();
	}

	private JButton demarer = new JButton("Démarrer");
	private JMenu menu = new JMenu("Fichier");
	private JMenuBar menuBar = new JMenuBar();
	private JMenuItem saveItem = new JMenuItem("Enregistrer");
	private JMenuItem loadItem = new JMenuItem("Ouvrir");

	/**
	 * Construit l'interface
	 */
	private void buildUI() {
		setLayout(new BorderLayout());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
		setTitle("Initialisation de la simulation");

		menuBar.add(menu);
		menu.add(loadItem);
		menu.add(saveItem);

		setJMenuBar(menuBar);

		JPanel centerPanel = new JPanel();
		centerPanel.add(piecePanel);
		add(centerPanel, BorderLayout.CENTER);
		add(modeChooser, BorderLayout.WEST);
		add(demarer, BorderLayout.SOUTH);
		pack();

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		setLocation((screenSize.width - getWidth()) / 2,
				(screenSize.height - getHeight()) / 2);

		addListeners();
	}

	/**
	 * Ajoute les listeners à l'interface
	 */
	private void addListeners() {
		demarer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					controleur.demarerSimulation();
				} catch (Exception e) {
					JOptionPane.showMessageDialog(InitialisationFrame.this,
							e.getMessage());
				}
			}
		});

		loadItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				final JFileChooser jFileChooser = new JFileChooser();
				final int returnValue = jFileChooser
						.showOpenDialog(InitialisationFrame.this);
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					File file = jFileChooser.getSelectedFile();
					try {
						controleur.load(file);
					} catch (FileNotFoundException e1) {
						JOptionPane.showMessageDialog(InitialisationFrame.this,
								"Fichier non trouvé.");
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(InitialisationFrame.this,
								"Impossible d'écrire dans ce fichier.");
					}
				}
			}
		});

		saveItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser jFileChooser = new JFileChooser();
				final int returnValue = jFileChooser
						.showSaveDialog(InitialisationFrame.this);
				jFileChooser.setVisible(true);
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					File file = jFileChooser.getSelectedFile();
					try {
						controleur.save(file);
					} catch (FileNotFoundException e1) {
						JOptionPane.showMessageDialog(InitialisationFrame.this,
								"Fichier non trouvé.");
					} catch (IOException e1) {
						JOptionPane.showMessageDialog(InitialisationFrame.this,
								"Impossible d'écrire dans ce fichier.");
					}
				}
			}
		});
	}

	public PieceListener getListener() {
		return this.piecePanel;
	}
}
